# -*- coding: utf-8 -*-
# ㊎ Author : XMZ
# ㊍ Time : 2021/10/14 9:41
# ㊌ File : formula -> universal.py
# ㊋ IDE :  PyCharm
# ㊏ 
import os
import sys
import time
import socket
import uuid
from snowflak import IdWorker


# 获取当前服务器的IP地址
def local_ip():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip


# 19 位 144开头: 1443034602078879744
def sonw19():
    time.sleep(0.00001)
    datacenter_id = 1
    worker_id = int(str(local_ip())[-1:])
    sonw = IdWorker(datacenter_id=datacenter_id, worker_id=worker_id).get_id()
    return sonw


# 17位 983开头: 98363914995384320
def sonw17():
    time.sleep(0.00001)
    datacenter_id = 2
    worker_id = int(str(local_ip())[-1:])
    sonw = IdWorker(datacenter_id=datacenter_id, worker_id=worker_id).get_id()
    return sonw


# 根据时间: 5bd670ce-29c8-3369-a8a1-10ce44c7259e
# 基于名字的MD5散列值。通过计算名字和命名空间的MD5散列值得到，保证了同一命名空间中不同名字的唯一性，和不同命名空间的唯一性，
# 但同一命名空间的同一名字生成相同的uuid。
def uuids3():
    da = time.time()
    results = uuid.uuid3(uuid.NAMESPACE_DNS, f"{da}")
    return results
